package com.hp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hp.entity.Patient;
import com.hp.entity.Register;
import com.hp.service.PatientService;
import com.hp.service.RegisterService;
import com.hp.util.ResultJson;
import com.hp.util.Utils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/register")
public class RegisterController {
    @Resource
    private RegisterService registerService;
    @RequestMapping("/findAll")
    private void findAll(Register register, @RequestParam(value = "page",defaultValue = "1")Integer page,
                         @RequestParam(value = "limit",defaultValue = "10")Integer limit, HttpServletResponse response){
        System.out.println(register);
        PageHelper.startPage(page,limit);
        List<Register> list = registerService.findAll(register);
        System.out.println(list);
        if (list.size() != 0) {
            Utils.returnJson(ResultJson.success(new PageInfo<>(list)), response);
        } else {
            Utils.returnJson(ResultJson.error(), response);
        }
    }
    @RequestMapping("/findById")
    private void findById(Register register, HttpServletResponse response){
        System.out.println(register);
        Register register1 = registerService.findById(register);
        System.out.println(register1);
        if (register1 != null) {
            Utils.returnJson(ResultJson.success(register1), response);
        } else {
            Utils.returnJson(ResultJson.error(), response);
        }
    }
    @RequestMapping("/delete")
    private void delete(String[] id, HttpServletResponse response){
        int i  = registerService.delete(id);
        if (i > 0) {
            Utils.returnJson(ResultJson.success(i), response);
        } else {
            Utils.returnJson(ResultJson.error(), response);
        }
    }
    @RequestMapping("/add")
    private void add(Register register, HttpServletResponse response){
        System.out.println(register);
        int i  = registerService.add(register);
        if (i > 0) {
            Utils.returnJson(ResultJson.success(i), response);
        } else {
            Utils.returnJson(ResultJson.error(), response);
        }
    }
    @RequestMapping("/update")
    private void update(Register register, HttpServletResponse response){
        int i  = registerService.update(register);
        if (i > 0) {
            Utils.returnJson(ResultJson.success(i), response);
        } else {
            Utils.returnJson(ResultJson.error(), response);
        }
    }
}
